package com.zzyk.main.service.impl;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzyk.main.model.pojo.AlarmMsgLog;
import com.zzyk.main.model.pojo.CommonCount;
import com.zzyk.main.model.vo.AlarmInfoToMonth;
import com.zzyk.main.model.vo.Message;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zzyk.main.mapper.AlarmInfoMapper;
import com.zzyk.main.model.pojo.AlarmInfo;
import com.zzyk.main.service.AlarmInfoService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wss_PC
 */
@Service
public class AlarmInfoServiceImpl implements AlarmInfoService {

    @Autowired
    private AlarmInfoMapper alarmInfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return alarmInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(AlarmInfo record) {
        return alarmInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(AlarmInfo record) {
        return alarmInfoMapper.insertSelective(record);
    }

    @Override
    public AlarmInfo selectByPrimaryKey(Integer id) {
        return alarmInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(AlarmInfo record) {
        return alarmInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AlarmInfo record) {
        return alarmInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message selectAlarmByParams(Long eId, Long dId, String beginTime, String endTime, Long status, String[] areas, Integer pageIndex, Integer pageSize, Integer[] deviceList) {
        /*开始时间默认上周*/
        Date bTime = StringUtils.isBlank(beginTime)
                && StringUtils.isBlank(endTime)
                ? DateUtil.beginOfDay(DateUtil.lastWeek())
                : StringUtils.isBlank(beginTime)
                && StringUtils.isNotBlank(endTime)
                ? DateUtil.offsetWeek(DateUtil.parseDate(endTime), -1)
                : DateUtil.parse(beginTime);
        Date eTime = StringUtils.isBlank(endTime)
                && StringUtils.isBlank(beginTime)
                ? DateUtil.date()
                : StringUtils.isBlank(endTime)
                && StringUtils.isNotBlank(beginTime)
                ? DateUtil.offsetWeek(bTime, 1)
                : DateUtil.parse(endTime);
        PageHelper.startPage(pageIndex, pageSize);
        List<AlarmInfo> list = alarmInfoMapper.selectAlarmInfoByParams(eId, dId, bTime, eTime, status, areas, deviceList);
        PageInfo<AlarmInfo> info = new PageInfo<AlarmInfo>(list);
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }

    @Override
    public List<CommonCount> findCountAlarm(Long enterprise, String[] areas, Long eId, Long dId, String beginTime, String endTime, Long status, Integer[] deviceList) {
        /*开始时间默认上周*/
        Date bTime = StringUtils.isBlank(beginTime)
                && StringUtils.isBlank(endTime)
                ? DateUtil.beginOfDay(DateUtil.lastWeek())
                : StringUtils.isBlank(beginTime)
                && StringUtils.isNotBlank(endTime)
                ? DateUtil.offsetWeek(DateUtil.parseDate(endTime), -1)
                : DateUtil.parse(beginTime);
        Date eTime = StringUtils.isBlank(endTime)
                && StringUtils.isBlank(beginTime)
                ? DateUtil.date()
                : StringUtils.isBlank(endTime)
                && StringUtils.isNotBlank(beginTime)
                ? DateUtil.offsetWeek(bTime, 1)
                : DateUtil.parse(endTime);
//        获取未处理警告和未恢复警告
        List<CommonCount> list = new ArrayList<>();
//        今日报警数量
        list.add(alarmInfoMapper.selectToDayAlarm(areas, enterprise,deviceList));
        list.add(alarmInfoMapper.selectThisweekAlarm(areas, enterprise,deviceList));
        /*根据条件查询未处理警告*/
        list.add(alarmInfoMapper.selectUntreatedAlarm(areas, enterprise, eId, dId, bTime, eTime,deviceList));
        /*根据条件查询未恢复告警*/
        list.add(alarmInfoMapper.selectNotRecoveredAlarm(areas, enterprise, eId, dId, bTime, eTime,deviceList));
//        本周报警数量
        return list;
    }

    @Override
    public List<AlarmMsgLog> selectAlarmMsgLog(Long eId, String beginTime, String endTime, String status, String[] areas, Integer type) {
        Date bTime = DateUtil.parse(beginTime);
        Date eTime = DateUtil.parse(endTime);
        if (ObjectUtil.isNotNull(bTime) && ObjectUtil.isNull(eTime)) {
            eTime = DateUtil.offsetMonth(bTime, 1);
        }
        if (ObjectUtil.isNotNull(eTime) && ObjectUtil.isNull(bTime)) {
            bTime = DateUtil.offsetMonth(eTime, -1);
        }
        if (ObjectUtil.isNotNull(type) && type == 0) {
            eId = null;
        }

        return alarmInfoMapper.selectAlarmMsgLog(eId, bTime, eTime, status, areas, type);
    }

    @Override
    public List<AlarmInfo> selectAlarmByParamsTop10(Long eId, Long dId, String beginTime, String endTime, Long status) {
        Date bTime = StringUtils.isBlank(beginTime)
                && StringUtils.isBlank(endTime)
                ? DateUtil.beginOfDay(DateUtil.lastWeek())
                : StringUtils.isBlank(beginTime)
                && StringUtils.isNotBlank(endTime)
                ? DateUtil.offsetWeek(DateUtil.parseDate(endTime), -1)
                : DateUtil.parse(beginTime);
        Date eTime = StringUtils.isBlank(endTime)
                && StringUtils.isBlank(beginTime)
                ? DateUtil.date()
                : StringUtils.isBlank(endTime)
                && StringUtils.isNotBlank(beginTime)
                ? DateUtil.offsetWeek(bTime, 1)
                : DateUtil.parse(endTime);
        return alarmInfoMapper.selectAlarmInfoByParamsTop10(eId, dId, bTime, eTime, status);
    }

    @Override
    public List<AlarmInfoToMonth> selectToMonthAlarmData(Integer enterprise, Integer[] deviceList) {
        return alarmInfoMapper.selectToMonthAlarmData(enterprise,deviceList);
    }

    @Override
    public List<Map<Integer, Integer>> findCountAlarmsByHour(String dateStr, Integer enterprise, Integer[] deviceList) {
        String yesTerDay;
        String toDay;
        if (StringUtils.isBlank(dateStr)) {
            toDay = DateUtil.today();
            yesTerDay = DateUtil.formatDate(DateUtil.yesterday());
        } else {
            toDay = dateStr;
            yesTerDay = DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parseDate(dateStr), -1));
        }
        return alarmInfoMapper.selectCountAlarmsByHour(toDay, yesTerDay, enterprise,deviceList);
    }

    @Override
    public List<Map<String, Integer>> countToDayAlarmNumber(String dateStr, Integer enterprise, Integer[] deviceList) {
        String day;
        if (StringUtils.isBlank(dateStr)) {
            day = DateUtil.today();
        } else {
            day = dateStr;
        }
        return alarmInfoMapper.countToDayAlarmNumber(day, enterprise,deviceList);
    }

    @Override
    public List<Map<String, Integer>> findAlarmNumberGroupByContent(String dateStr, Integer enterprise, Integer[] deviceList) {
        String day;
        if (StringUtils.isBlank(dateStr)) {
            day = DateUtil.today();
        } else {
            day = dateStr;
        }
        return alarmInfoMapper.selectAlarmNumberGroupByContent(day, day, enterprise,deviceList);
    }

    @Override
    public List<Map<String, Integer>> selectAlarmDeviceType(String dateStr, Integer enterprise, Integer[] deviceList) {
        if (StringUtils.isBlank(dateStr)) {
            dateStr = DateUtil.today();
        }
        return alarmInfoMapper.selectAlarmDeviceType(dateStr, dateStr, enterprise,deviceList);
    }

    @Override
    public List<Map<String, Integer>> selectAlarmStatistics(String dateStr, Integer enterprise, Integer[] deviceList) {
        String beginDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] split = dateStr.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return alarmInfoMapper.selectAlarmStatistics(beginDate, endDate, enterprise,deviceList);
    }

    @Override
    public List<Map<String, Integer>> selectAlarmHandlingInformation(String dateStr, Integer enterprise, Integer[] deviceList) {
        String beginDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] split = dateStr.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return alarmInfoMapper.selectAlarmHandlingInformation(beginDate, endDate, enterprise,deviceList);
    }

    @Override
    public List<Map<String, Integer>> selectAlarmCauseDistribution(String dateStr, Integer enterprise, Integer[] deviceList) {
        String beginDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] split = dateStr.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return alarmInfoMapper.selectAlarmNumberGroupByContent(beginDate, endDate, enterprise, deviceList);
    }

    @Override
    public List<Map<String, Integer>> selectHistoryAlarmDeviceType(String dateStr, Integer enterprise, Integer[] deviceList) {
        String beginDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] split = dateStr.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return alarmInfoMapper.selectAlarmDeviceType(beginDate, endDate, enterprise, deviceList);
    }

    @Override
    public Message selectRecentAlertNotification(Integer type, Integer enterprise, Integer pageIndex, Integer pageSize, Integer methodType) {
        String dateStr = DateUtil.offsetDay(new Date(), -4).toDateStr();
        PageHelper.startPage(pageIndex, pageSize);
        PageInfo<AlarmInfo> info = new PageInfo<>(alarmInfoMapper.selectRecentAlertNotification(type, enterprise, dateStr, methodType));
        return Message.success()
                .addData("data", info.getList())
                .addData("total", info.getTotal())
                .addData("pages", info.getPages());
    }

    @Override
    public List<AlarmInfo> getAlarmCountBy(String dateStr, Integer enterprise, Integer[] deviceList) {
        if (StringUtils.isBlank(dateStr)) {
            dateStr = DateUtil.today();
        }
        return alarmInfoMapper.selectAlarmCountBy(dateStr, dateStr, enterprise, deviceList);
    }

    @Override
    public List<AlarmInfo> getCountHistoryAlarmDeviceType(String dateStr, Integer enterprise, Integer[] deviceList) {
        String beginDate = DateUtil.formatDate(DateUtil.offsetDay(new Date(), -29));
        String endDate = DateUtil.today();
        if (StringUtils.isNotBlank(dateStr)) {
            String[] split = dateStr.split(" ~ ");
            beginDate = split[0];
            endDate = split[1];
        }
        return alarmInfoMapper.selectAlarmCountBy(beginDate, endDate, enterprise,deviceList);
    }

    @Override
    public List<AlarmInfo> apiSelectAlarmByParams(Long eId, Long dId, String beginTime, String endTime, Long status, String[] areas, Integer[] deviceList) {
        /*开始时间默认上周*/
        Date bTime = StringUtils.isBlank(beginTime)
                && StringUtils.isBlank(endTime)
                ? DateUtil.beginOfDay(DateUtil.lastWeek())
                : StringUtils.isBlank(beginTime)
                && StringUtils.isNotBlank(endTime)
                ? DateUtil.offsetWeek(DateUtil.parseDate(endTime), -1)
                : DateUtil.parse(beginTime);
        Date eTime = StringUtils.isBlank(endTime)
                && StringUtils.isBlank(beginTime)
                ? DateUtil.date()
                : StringUtils.isBlank(endTime)
                && StringUtils.isNotBlank(beginTime)
                ? DateUtil.offsetWeek(bTime, 1)
                : DateUtil.parse(endTime);
        return alarmInfoMapper.selectAlarmInfoByParams(eId, dId, bTime, eTime, status, areas,deviceList);
    }

}
